package com.IntelligenceInsight.Myth.service;

import com.IntelligenceInsight.Myth.mapper.AlgorithmMapper;
import com.IntelligenceInsight.Myth.model.Algorithm;
import com.IntelligenceInsight.Myth.util.NoteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AlgorithmService {
    @Autowired
    private AlgorithmMapper mapper;

    public NoteRequest AlgorithmAdd(String uuid, String name, String owner, String description, String defaultParams) {
        NoteRequest res = new NoteRequest();
        int n = mapper.AlgorithmAdd(uuid, name, owner, description, defaultParams);
        if(n < 1) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success: " + uuid);
        }
        return res;
    }

    public NoteRequest AlgorithmUpdate(String uuid, int lastVersion, int version, String jarPath) {
        NoteRequest res = new NoteRequest();
        int n = mapper.AlgorithmUpdate(uuid, lastVersion, version, jarPath);
        if(n < 1) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success: " + uuid);
        }
        return res;
    }


    public NoteRequest AlgorithmListByOwner(String owner) {
        NoteRequest res = new NoteRequest();
        List<Algorithm> algorithm = mapper.AlgorithmListByOwner(owner);
        if(algorithm == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(algorithm);
        }
        return res;
    }

    public NoteRequest AlgorithmListAll(int page,int limit) {
        NoteRequest res = new NoteRequest();
        List<Algorithm> algorithm = mapper.AlgorithmListAll(page,limit);
        if(algorithm == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(algorithm);
        }
        return res;
    }

    public NoteRequest AlgorithmListAllforName() {
        NoteRequest res = new NoteRequest();
        List<Algorithm> algorithm = mapper.AlgorithmListAllforName();
        if(algorithm == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(algorithm);
        }
        return res;
    }

    public int AlgorithmListAllCount() {
        NoteRequest res = new NoteRequest();
        int count = mapper.AlgorithmListAllCount();
        return count;
    }

    public NoteRequest AlgorithmByUuid(String uuid) {
        NoteRequest res = new NoteRequest();
        Algorithm algorithm = mapper.AlgorithmByUuid(uuid);
        if(algorithm == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(algorithm);
        }
        return res;
    }

    public NoteRequest AlgorithmByName(String name) {
        NoteRequest res = new NoteRequest();
        Algorithm algorithm = mapper.AlgorithmByName(name);
        if(algorithm == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(algorithm);
        }
        return res;
    }

    public NoteRequest AlgorithmRemoveByName(String name) {
        NoteRequest res = new NoteRequest();
        int n = mapper.AlgorithmRemoveByName(name);
        if(n < 1) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
        }
        return res;
    }
}
